package com.music.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.music.mapper.PlaylistMapper;
import com.music.mapper.SingleSongMapper;
import com.music.pojo.Playlist;
import com.music.pojo.PlaylistExample;
import com.music.pojo.SingleSong;
import com.music.service.PlayListService;
import com.music.utils.PlayListInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PlayListServiceImpl implements PlayListService {
    @Autowired
    private PlaylistMapper playlistMapper;

    @Autowired
    private SingleSongMapper singleSongMapper;
    @Override
    public PageInfo<Playlist> findPlaylistBySearch(String search, Integer pageNum) {
        PageHelper.startPage(pageNum,10);
        List<Playlist> playlistBySearch = playlistMapper.findPlaylistBySearch(search);
        PageInfo<Playlist> pageInfo = new PageInfo<>(playlistBySearch);
        return pageInfo;
    }


    public PageInfo<Playlist> find(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Playlist> playlistBySearch = playlistMapper.findAll();
        PageInfo<Playlist> pageInfo = new PageInfo<>(playlistBySearch);
        return pageInfo;
    }

    @Override
    public PageInfo<Playlist> findByMid(Integer pageNum, Integer pageSize, Integer mid) {
        PageHelper.startPage(pageNum,pageSize);
        List<Playlist> playlistBySearch = playlistMapper.findByMid(mid);
        return new PageInfo<>(playlistBySearch);
    }

    @Override
    public List<Playlist> findByUid(Integer uid) {
        return playlistMapper.findByUid(uid);
    }

    @Override
    public void removeFavorite(Integer mid, Integer uid) {
        playlistMapper.removeFavorite(mid,uid);
    }

    @Override
    public void addFavorite(Integer mid, Integer plid) {
        playlistMapper.addFavorite(mid,plid);
    }

    @Override
    public void addFavoriteToNew(Integer mid, String plname, Integer uid) {
        addNewPlayList(plname,uid);
        Integer maxPlid = playlistMapper.findMaxPlid();
        String playListPic = playlistMapper.findPlayListPic(maxPlid);
        if (playListPic == null){
            updatePlayListPic(mid,maxPlid);
        }
        addFavorite(mid,maxPlid);
    }

    @Override
    public void addNewPlayList(String plname, Integer uid) {
        playlistMapper.addNewPlayList(plname,uid);
    }

    @Override
    public void updatePlayListPic(Integer mid,Integer plid) {
        String pic = singleSongMapper.findPicByMid(mid);
        playlistMapper.updatePlayListPic(pic,plid);
    }

    @Override
    public PageInfo<Playlist> findPlaylistByCategory(String category,Integer pageNum) {
        PageHelper.startPage(pageNum,20);
        List<Playlist> playlistByCategory = new ArrayList<>();
        if(category.equals("1")){
            playlistByCategory = playlistMapper.findPlaylistByCategoryAll();
        }else {
            playlistByCategory = playlistMapper.findPlaylistByCategory(category);
        }
        PageInfo<Playlist> pageInfo = new PageInfo<>(playlistByCategory);
        return pageInfo;
    }

    @Override
    public Playlist findDetailByPlid(Integer plid, Integer uid) {
        Playlist playlist = playlistMapper.findByPlid(plid);
        List<SingleSong> list = singleSongMapper.findByplid(plid);
        for (SingleSong singleSong : list) {
            if (uid==null){
                singleSong.setFavoriteStatus(0);
            }else {
                singleSong.setFavoriteStatus(singleSongMapper.isFavorite(singleSong.getMid(), uid));
            }
        }
        Integer num;
        if (uid==null){
            num = 0;
        }else {
            num= playlistMapper.findisFavorite(plid,uid);
        }
        playlist.setSongs(list);
        playlist.setFavoriteStatus(num);
        return playlist;
    }

    @Override
    public void addFavoritePlayList(Integer plid, Integer uid) {
        playlistMapper.addFavoritePlayList(plid,uid);
    }

    @Override
    public void removeFavoritePlayList(Integer plid, Integer uid) {
        playlistMapper.removeFavoritePlayList(plid,uid);
    }

    @Override
    public void removePlayList(Integer plid) {
        playlistMapper.removePlayList(plid);
    }

    @Override
    public void addOrUpdatePlayList(PlayListInfo playListInfo, Integer uid) {
        List<String> cates = playListInfo.getCates();
        if (playListInfo.getPlid()==null){
            playlistMapper.addNewPlayListWithDetail(playListInfo.getPlname(),playListInfo.getDesc(),playListInfo.getPicUrl(),uid);
            Integer maxPlid = playlistMapper.findMaxPlid();
            if (cates!=null){
                for (String cate : cates) {
                    playlistMapper.addPlayListCateGory(cate,maxPlid);
                }
            }
        }else {
            playlistMapper.updatePlayListInfoActive(playListInfo);
            playlistMapper.deletePlayListCateGoryByPlid(playListInfo.getPlid());
            if (cates!=null){
                for (String cate : cates) {
                    playlistMapper.addPlayListCateGory(cate,playListInfo.getPlid());
                }
            }
        }
    }
}
